package com.hiwhitley.himei.module.meitu;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hiwhitley.himei.R;
import com.hiwhitley.himei.entity.Meitu;
import com.hiwhitley.himei.widget.glide.GlideImageView;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.drakeet.multitype.ItemViewProvider;

/**
 * Created by hiwhitley on 17-2-26.
 */
public class MeituViewProvider
        extends ItemViewProvider<Meitu, MeituViewProvider.ViewHolder> {

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_meitu, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull Meitu meitu) {
        holder.mMeituView.loadNetImage(meitu.getUrl(), R.drawable.bg_holder_brown);
        holder.mMeituView.setOnClickListener(v -> toNextActivity(holder, meitu));
    }

    private void toNextActivity(ViewHolder holder, Meitu meitu) {
        Context context = holder.mMeituView.getContext();
        Intent intent = PhotoActivity.newIntent(context, meitu.getUrl());

        ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(
                (Activity) context, holder.mMeituView, PhotoActivity.TRANSIT_PIC);
        try {
            ActivityCompat.startActivity(context, intent, optionsCompat.toBundle());
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            context.startActivity(intent);
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_mei_tu)
        GlideImageView mMeituView;

        ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}